package com.pzr.soccer.uitl;


import com.pzr.soccer.domain.Expression;
import com.pzr.soccer.domain.Plan;
import com.pzr.soccer.domain.game.GameWrapper;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author pzr
 * @date:2021-12-09-17:36
 * @Description: 测试game wrapper 里面的game是否符合plan
 **/
@Slf4j
public class FindRightGameForPlan {

    public static boolean testCondition(GameWrapper wrapper, Plan plan){

             //currentTime 不在测试范围
             if(plan.getCondition_begin()>wrapper.getCurrentMin_revised()||plan.getCondition_end()<wrapper.getCurrentMin_revised()){
                // log.info(wrapper.getCurrentMin_revised()+" not in period: expect: ["+plan.getCondition_begin()+","+plan.getCondition_end()+"]");
                 return false;
             }
             int begin=plan.getCondition_begin();
             int end=wrapper.getCurrentMin_revised();

             for(Expression expression:plan.getPre_conditions()){
                 if (testExpression(wrapper, begin, end, expression)) return false;
             }

       return true;
    }


    public static boolean testResult(GameWrapper wrapper,Plan plan){

            //currentTime 不在测试范围
            if(plan.getResult_begin()>wrapper.getCurrentMin_revised()||plan.getResult_end()<wrapper.getCurrentMin_revised()){
              //  log.info(wrapper.getCurrentMin_revised()+" not in period: expect: ["+plan.getResult_begin()+","+plan.getResult_end()+"]");
                return false;
            }
            int begin=plan.getCondition_begin();
            int end=wrapper.getCurrentMin_revised();

            for(Expression expression:plan.getResult_conditions()){
                if (testExpression(wrapper, begin, end, expression)) return false;
            }

        return true;
    }

    private static boolean testExpression(GameWrapper wrapper, int begin, int end, Expression expression) {
        Object real_result= MatchCalculator.calculate(expression.getLeft(),wrapper.getGame(),new int[]{begin,end});
       // log.info(expression.getLeft()+" "+expression.getOperator()+" "+expression.getRight()+"  realResult:"+real_result);
        if(!Comparer.compare(expression.getOperator(),real_result,expression.getRight())){
           // log.info("failed, expect:"+expression+"  but come out: "+real_result);
            return true;
        }
        return false;
    }

}
